<?php
$parentCaption = BUtils::T('loyalty_program_lbl');
$caption = BUtils::T('points_to_customer_lbl');
$this->pageTitle = $caption;

$this->breadcrumbs = array(
	$parentCaption => array('/program'),
	$caption,
);

$points_s_lbl = BUtils::T('points_s_lbl');
$visits_lbl = BUtils::T('visits_lbl');
$cust_default_face = Yii::app()->baseUrl . '/img/user-128.png';

$notify_lang = Yii::app()->user->lang;
if (isset(Yii::app()->user->lang_notify)) $notify_lang = Yii::app()->user->lang_notify;
?>
<div class="page-header position-relative">
	<div class="pull-right">
		<a class="btn btn-success" href="<?php echo Yii::app()->createUrl('/points/from'); ?>">
			<i class="icon-trophy bigger-120"></i>
			<?php echo BUtils::T('points_from_customer_lbl'); ?>
		</a>
	</div>
	<h1>
		<?php echo $parentCaption; ?>
		<small>
			<i class="icon-double-angle-right"></i>
			<?php echo $caption; ?>
		</small>
	</h1>
</div><!--/.page-header-->

<div class="row-fluid">
	<!-- Message -->
	<div class="alert alert-block alert-info">
		<button type="button" class="close" data-dismiss="alert">
			<i class="icon-remove"></i>
		</button>
		<i class="icon-info-sign"></i>
		<?php echo BUtils::T('points_from_customer_eg'); ?>
	</div>
	
	<div class="space-6"></div>
	
	<div class="row-fluid">
		<div class="span6">
			<div class="widget-box transparent" data-spy="affix-bottom">
				<div class="widget-header">
					<h3 class="lighter blue"><i class="icon-signin"></i> <?php echo BUtils::T('points_to_customer_lbl'); ?></h3>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
						<div class="widget-main" style="padding:0; padding-top:12px;">
							<div class="well bg-blue">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'assign-form',
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); ?>
	<h4 class="">
		<?php echo BUtils::T('points_lbl'); ?>
	</h4>
	<?php echo $form->textField($model, 'points', array('id'=>'assign-points', 'class'=>'xlarge span12', 'placeholder'=>BUtils::T('points_how_many_eg'), 'style'=>'height:auto;', 'autocomplete'=>'off')); ?>
	<?php echo $form->error($model,'points', array('id'=>'assign-points-error')); ?>
	<div class="space-6"></div>
	<h4 class="">
		<?php echo BUtils::T('customer_lbl'); ?>
	</h4>
	<div class="relative">
		<?php echo $form->dropdownList($model, 'customer', array(), array('id'=>'assign-customer', 'class'=>'xlarge', 'placeholder'=>BUtils::T('customer_info_eg'))); ?>
	</div>
	<?php echo $form->error($model,'customer', array('id'=>'assign-customer-error')); ?>
	<div class="space-6"></div>
	<div class="row-fluid">
		<div class="span4">
			<?php echo CHtml::ajaxSubmitButton(BUtils::T('confirm_btn'), Yii::app()->createUrl($this->id.'/assign'), array(
				'dataType' => 'html',
				'beforeSend' => 'js:function(){beforeSubmit("#assign");}',
				'error' => 'js:function(resp){errorSubmit(resp, "#assign");}',
				'success' => 'js:function(data){afterSubmit(data, "#assign");}',
			), array('class'=>'btn btn-large btn-blue', 'id'=>'assign-button')); ?>
			<img id="assign-button-loader" style="display:none;" src="<?php echo Yii::app()->baseUrl; ?>/img/ajax-loaders/ajax-loader-4.gif" />
		</div>
<?php if (substr(Yii::app()->language, 0, 2) != 'fi') { ?>
		<div class="pull-right">
			<label>
				<i class="icon-question-sign" rel="tipsy-top" data-toggle="tooltip" title="<?php echo BUtils::T('points_notification_language_eg') ?>"></i>
				<?php echo BUtils::T('points_notification_language_lbl') ?>:
				<?php echo CHtml::dropDownList('notify[lang]', $notify_lang, array('fi_fi'=>'Suomi','en_gb'=>'English','zh_cn'=>'简体中文'), array('style'=>'width:auto;')); ?>
			</label>
		</div>
<?php } ?>
	</div>
<?php $this->endWidget(); ?>
								<hr class="" />
								<div id="assign-result">
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="span6">
			<div class="widget-box transparent">
				<div class="widget-header">
					<h3 class="lighter blue"><i class="icon-group"></i> <?php echo BUtils::T('customers_recent_lbl'); ?></h3>
					<div class="widget-toolbar">
						<a href="<?php echo Yii::app()->createUrl('/customer'); ?>">
							<i class="icon-list"></i> <?php echo BUtils::T('customer_list_lbl'); ?>
						</a>
					</div>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
						<div class="widget-main" style="padding:0;">
							<div id="customer-list-loader" class="text-center" style="display:none;">
								<img src="<?php echo Yii::app()->baseUrl; ?>/img/ajax-loaders/ajax-loader-4.gif" />
							</div>
							<div id="customer-list">
								<!-- AJAX Loading -->
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
jQuery(function() {
	//load customer list
	loadCustomer();
	
	$('input[type="text"]').on('blur', function(){
		var errorId = $(this).attr('id') + '-error';
		$('#' + errorId).hide();
	});
	
	//select customer
	$('#assign-customer').selectize({
		valueField: 'id',
		labelField: 'name',
		searchField: ['id','name','mobile','email'],
		create: true, persist: false,
		render: {
			item: function(item, escape) {
				if (item.img) {
					return '<div class="selected-customer">' +
						'<img class="small-img" src="' + escape(item.img) + '" alt="" /> ' +
						'<span class="small-title">' + escape(item.name) + (item.id ? ' ('+escape(item.hitted)+')' : '') + '</span>' +
					'</div>';
				} else {
					return '<div class="selected-customer">' +
						'<span class="small-title">' + escape(item.name) + '</span>' +
					'</div>';
				}
			},
			option: function(item, escape) {
				if (item.img) {
					return '<table class="table table-hover table-customer" style="margin-bottom:0;"><tr>' +
						'<td class="user" style="width:48px;"><img src="' + escape(item.img) + '" alt="" /></td>' +
						'<td class="uinfo">' +
							'<div class="cust-name"><span class="blue">' + escape(item.name) + '</span></div>' +
							'<div class="cust-contact">' + item.contact + '</div>' +
							'<div class="visit-time">' + escape(item.last_time) + '</div>' +
						'</td>' +
						'<td class="uvisits" style="width:64px;">' +
							'<div class="center text-big blue">' + escape(item.visit_times) + '</div>' +
							'<div class="center"><?php echo $visits_lbl; ?></div>' +
						'</td>' +
						'<td class="upoints" style="width:64px;">' +
							'<div class="center text-big blue">' + escape(item.points) + '</div>' +
							'<div class="center"><?php echo $points_s_lbl; ?></div>' +
						'</td>' +
					'</tr></table>';
				} else {
					return '<table class="table table-hover table-customer" style="margin-bottom:0;"><tr>' +
						'<td class="user" style="width:48px;"><img src="<?php echo $cust_default_face; ?>" alt="" /></td>' +
						'<td class="uinfo">' +
							'<div class="cust-name"><span class="blue">' + escape(item.name) + '</span></div>' +
							'<div class="visit-time"></div>' +
						'</td>' +
					'</tr></table>';
				}
			}
		},
		load: function(query, callback) {
			if (!query.length) return callback();
			$.ajax({
				url:'<?php echo Yii::app()->createUrl('customer/suggest', array('type'=>'json')) ?>',
				data:{'q':query}, dataType: 'json',
				error: function() {
					callback();
				},
				success: function(res) {
					callback(res.customers);
				}
			});
		}
	});
});

function beforeSubmit(eId) {
	$(eId + '-button').attr('disabled', true);
	$(eId + '-button-loader').show();
	$(eId + '-customer-error').html('');
}
function errorSubmit(res, eId) {
	$(eId + '-button').attr('disabled', false);
	$(eId + '-button-loader').hide();
	var data = JSON.parse(res.responseText);
	for (var prop in data) {
		$(eId+'-'+prop).focus();
		if (data.hasOwnProperty(prop)){
			$(eId+'-'+prop+'-error').show().html(data[prop][0]);
		}
		return;
	}
}
function afterSubmit(data, eId) {
	$(eId + '-points').val('');
	$(eId + '-button').attr('disabled', false);
	$(eId + '-button-loader').hide();
	$(eId + '-result').html(data);
	$(eId + '-button').parent().find('input[type="text"]').val('');//clear old value
	loadCustomer();
	//notify: when merchant choose to the notification option
	$.bootstrapGrowl('A notification has been sent to this customer!', {type:'success'});
}
//load customer recent list
function loadCustomer() {
	$.ajax({
		type:'GET', url:'<?php echo Yii::app()->createUrl('customer/list') ?>', data:{'sort':'recent', 'limit':5},
		beforeSend:function() {
			$('#customer-list-loader').show();
		},
		success:function(data) {
			$('#customer-list-loader').hide();
			$('#customer-list').html(data);
		}
	});
}
</script>